<?php


namespace App\Logic;


use App\Models\News;
use App\Models\Store;

class StoreLogic
{
    /**
     *门店数据展示
     * @yuankai
     */
    public static function list()
    {
        if (empty($paginate)){
            $paginate = 10;
        }
        return Store::list($paginate);
    }

    /**
     * 门店数据添加
     * @yuankai
     */
    public static function add($request)
    {
        $validated = $request->validate([
            'name' => 'required',
            'address' => 'required',
            'province' => 'required',
            'city' => 'required',
            'district' => 'required',
        ],[
            'name.required'=>'不能为空',
            'address.required'=>'不能为空',
            'province.required'=>'不能为空',
            'city.required'=>'不能为空',
            'district.required'=>'不能为空',
        ]);
        $res = Store::create($validated);
        return $res;
    }

    /**
     * 门店数据删除
     * @yuankai
     */
    public static function del($store_id){
        if(empty($store_id)){
            throw new \Exception('未接收到想要删除的门店信息');
        }
        $del=Store::del('id',$store_id);
        return $del;
    }

    /**
     * 门店数据查找
     * @yuankai
     */
    public static function search($store_name){
        if(empty($store_name)){
            return Store::list();
        }
        if (!empty($store_name)){
            $where[] = ['name','like',"%$store_name%"];
        }
        return Store::search($where);
    }

    /**
     * 门店详情
     * @yuankai
     */
    public static function details($id){
        if(empty($id)){
            throw new \Exception('未接收到想要查看的门店信息');
        }
        $res = Store::detail('id',$id);
        return $res;
    }

    /**
     * 门店数据修改
     * @yuankai
     */
    public static function modify($update){
        unset($update['updated_at']);
        unset($update['created_at']);
        unset($update['deleted_at']);
        $id = $update['id'];
        $res = Store::modify('id',$id,$update);
        return $res;
    }
}
